Component({
  options:{
    styleIsolation:'shared'
  },
	data: {
    hidden: false,
		active: 0,
		list: []
	},

	methods: {
		onChange(event) {
      this.setData({ active: event.detail });
      console.log(event);
			wx.switchTab({
        url: this.data.list[event.detail].url,
        success: () => {
          console.log("跳转成功");
        },
        fail: (err) => {
          console.error("跳转失败:", err);
        }
      });
		},

		// init() {
		// 	const page = getCurrentPages().pop();
		// 	this.setData({
		// 		active: this.data.list.findIndex(item => item.url === `/${page.route}`)
		// 	});
    // },
    init() {
      const userRole = wx.getStorageSync('urlPath')|| 'employee';
      const tabs = this.getTabsByRole(userRole);
      this.setData({ list: tabs });
      const page = getCurrentPages().pop();
      console.log(page);
      const activeIndex = tabs.findIndex(item => item.url === `/${page.route}`);
      console.log(activeIndex);
      this.setData({ active: activeIndex });
      
    },

    getTabsByRole(role) {
      console.log(role);
      const tabs = [
        {
          icon: 'shop-o',
          text: '商城',
          url: '/pages/shop/shop',
          role:'employee'
        },
        {
          icon: 'shopping-cart-o',
          text: '购物车',
          url: '/pages/shopping_cart/shopping_cart',
          role:'employee'
        },
        {
          icon: 'user-o',
          text: '我的',
          url: '/pages/user_profile/user_profile',
          role:'employee'
        },
        {
          icon: 'shop-o',
          text: '商城',
          url: '/pages/shop/shop',
          role:'admin'
        },
        {
          icon: 'shopping-cart-o',
          text: '购物车',
          url: '/pages/shopping_cart/shopping_cart',
          role:'admin'
        },
        {
          icon: 'user-o',
          text: '我的',
          url: '/pages/user_profile/user_profile',
          role:'admin'
        },
        {
          icon: 'shop-o',
          text: '商城管理',
          url: '/pages/shop-manage/shop-manage',
          role:'sadmin'
        },
        {
          icon:'manager-o',
          text:'设置管理员',
          url:'/pages/set_admin/set_admin',
          role:'sadmin'
        },
        {
          icon: 'user-o',
          text: '个人中心',
          url: '/pages/user_profile/user_profile',
          role:'sadmin'
        },
      ];
      return tabs.filter(tab => tab.role === role);
    },
    attached() {
      this.init();
    }
	}
});